/**
 * 获取你传入的type类型的数据
 */

const {
  Movie,
  Music,
  Sentence
} = require('@model/classic.js')
const { ArtNotExistException } = require('@core/http-exception.js')
const { Op } = require('sequelize')
const { ClassicType } = require('@lib/enum')
const { flatten } = require('lodash')
class Art {
  // 获取详情
  static async getDetail (artId, type) {
    const finder = {
      where: {
        id: artId,
        type
      }
    }
    let art
    type = parseInt(type)
    switch (type) {
      case 100:
        art = await Movie.findOne(finder)
        break
      case 200:
        art = await Music.findOne(finder)
        break
      case 300:
        art = await Sentence.findOne(finder)
        break
      case 400:
        const { Book } = require('@model/book.js')
        let book = await Book.findOne({
          where: {
            id: artId,
            type
          }
        })
        if (!book) {
          book = await Book.create({
            id: artId,
            type
          })
        }
        let art1 = await Book.getDetail(artId)
        if (!art1) {
          throw new ArtNotExistException('查不到数据')
        }
        art = book
        art.setDataValue('result', art1.result)
        break
    }
    if (!art) {
      throw new ArtNotExistException('查询不到此数据')
    }
    return art
  }
  // 获取作品的点赞数量/作品是否在favor表里
  static async artLikeStatus (artId, type, uid) {
    let artWithme = {
      iLike: false,
      favNums: 0
    }
    // 查询art
    const art = await Art.getDetail(artId, type)
    if (!art) {
      throw new ArtNotExistException('查询不到此数据')
    }
    artWithme.favNums = art.fav_nums
    const { Favor } = require('@model/favor.js')
    const favor = await Favor.findOne({
      where: {
        artId,
        type,
        uid
      }
    })
    artWithme.iLike = favor ? true : false
    return artWithme
  }
  static async getList (type) {
    const { Flow } = require('@model/flow.js')
    const flowList = await Flow.findAll()
    // console.log(flowList)
    // id分类
    let typeIdsObj = {
      100: [],
      200: [],
      300: []
    }
    for (let flow of flowList) {
      typeIdsObj[flow.type].push(flow.artId)
    }
    type = parseInt(type)
    if (ClassicType.isThisType(type)) {
      return await Art.getListByType(type, typeIdsObj[type])
    } else if (type === 0) {
      let arr = []
      for (let key in typeIdsObj) {
        console.log('key', key);
        console.log('typeIdsObj[key]', typeIdsObj[key]);
        arr.push(await Art.getListByType(key, typeIdsObj[key]))
      }
      return flatten(arr)
    } else {
      throw new ArtNotExistException('没有查询到数据')
    }
  }
  static async getListByType (type, ids) {
    type = parseInt(type)
    let arts
    const finder = {
      where: {
        id: {
          [Op.in]: ids
        }
      }
    }
    switch (type) {
      case 100:
        arts = await Movie.findAll(finder)
        break;
      case 200:
        arts = await Music.findAll(finder)
        break;
      case 300:
        arts = await Sentence.findAll(finder)
        break;
    }
    console.log(arts);
    return arts
  }
  // 获取上一期
  static async getPrevious (index) {
    index = parseInt(index)
    // 获取当前目录的最大值
    const { Flow } = require('@model/flow.js')
    const max = await Flow.count()
    const flow = await Flow.findOne({
      where: {
        index: index > 1 ? index - 1 : max
      }
    })
    if (!flow) {
      throw new ArtNotExistException('没有查询到数据')
    }
    const art = await Art.getDetail(flow.artId, flow.type)
    if (!art) {
      throw new ArtNotExistException('没有查询到数据')
    }
    await art.setDataValue('index', flow.index)
    return art
  }
  // 获取下一期
  static async getNext (index) {
    index = parseInt(index)
    const { Flow } = require('@model/flow.js')
    // 获取总数量
    const max = await Flow.count()
    const flow = await Flow.findOne({
      where: {
        index: index === max ? 1 : index + 1
      }
    })
    if (!flow) {
      throw new ArtNotExistException('查不到这个数据')
    }
    const art = await Art.getDetail(flow.artId, flow.type)
    if (!art) {
      throw new ArtNotExistException('查不到这个数据')
    }
    await art.setDataValue('index', flow.index)
    return art
  }
}

module.exports = {
  Art
}